package com.vegetable.modules.controller.baseData;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.baseData.BaseDict;
import com.vegetable.modules.entity.baseData.ProductCostClause;
import com.vegetable.modules.service.baseData.IBaseDictService;
import com.vegetable.modules.service.baseData.IProductCostClauseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 商品代办包装费用标准 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-05
 */
@Slf4j
@RestController
@RequestMapping("/productCostClause")
@AllArgsConstructor
@Api(value = "商品代办包装费用标准", tags = "商品代办包装费用标准")
public class ProductCostClauseController {

    private IProductCostClauseService productCostClauseService;
    private IBaseDictService baseDictService;

    /**
     * 所有代办包装费用标准列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询产地商品的代办包装费用标准列表", notes = "查询产地商品的代办包装费用标准列表")
    public R list(@RequestParam Map<String, Object> params){
        String producerId = (String)params.get("producerId");
        if (StringUtils.isBlank(producerId)){
            return R.fail("商品id不能为空");
        }
        log.info("查询产地商品的代办包装费用标准列表{}", JSON.toJSONString(params));
        PageUtils page = productCostClauseService.queryPage(params);

        return R.data( page);
    }

    /**
     * 新增代办包装费用标准
     */
    @SysLog(value = "新增代办包装费用标准")
    @PostMapping("/save")
    @ApiOperation(value = "新增产地商品的代办包装费用标准", notes = "新增产地商品的代办包装费用标准")
    public R save(@RequestBody ProductCostClause productCostClause){
        log.info("新增产地商品的代办包装费用标准{}", JSON.toJSONString(productCostClause));
        if (productCostClause.getProducerId() == null){
            return R.fail("商品id不能为空");
        }
        BaseDict byId = baseDictService.getById(productCostClause.getCostClauseId());
        if (byId == null){
            return R.fail("包装费用科目不存在");
        }
        productCostClause.setCostClauseName(byId.getValueOne());
        productCostClauseService.save(productCostClause);
        return R.status(true);
    }

    /**
     * 修改代办包装费用标准
     */
    @SysLog(value = "修改代办包装费用标准")
    @PostMapping("/update")
    @ApiOperation(value = "修改产地商品的代办包装费用标准", notes = "修改产地商品的代办包装费用标准")
    public R update(@RequestBody ProductCostClause productCostClause){
        log.info("修改产地商品的代办包装费用标准{}", JSON.toJSONString(productCostClause));
        if (productCostClause.getProducerId() == null || productCostClause.getId() == null){
            return R.fail("商品id和代办包装费用标准id都不能为空");
        }
        BaseDict byId = baseDictService.getById(productCostClause.getCostClauseId());
        if (byId == null){
            return R.fail("包装费用科目不存在");
        }
        // 是材料  & 是否统一更新为【是】
        if (productCostClause.getIsUnifyUpdate() == 1){
            UpdateWrapper<ProductCostClause> updateWrapper = new UpdateWrapper();
            updateWrapper.lambda().eq(ProductCostClause::getCostClauseId, productCostClause.getCostClauseId());
            updateWrapper.lambda().set(ProductCostClause::getDefaultPrice, productCostClause.getDefaultPrice());
            productCostClauseService.getBaseMapper().update(null, updateWrapper);
        }

        productCostClause.setCostClauseName(byId.getValueOne());
        productCostClauseService.updateById(productCostClause);
        return R.status(true);
    }

    /**
     * 删除代办包装费用标准
     */
    @SysLog(value = "删除代办包装费用标准")
    @GetMapping("/delete")
    @ApiOperation(value = "根据代办包装费用标准id删除代办包装费用标准", notes = "根据代办包装费用标准id删除代办包装费用标准")
    public R delete(Long id){
        productCostClauseService.removeById(id);
        return R.status(true);
    }
}
